package conf;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;

import logger.ApplicationLogger;

public class PropReaderWriter {


	private static Properties configuration;
	private static Properties xlsConfiguration;
	private static Properties keyBindings;
	private static Properties lang;

	private static String confUrl = "conf/settings.properties" ;
	private static String xlsUrl = "conf/xls.properties";
	private static String langUrl = "conf/lang.properties";
	private static String keybindingsUrl = "conf/keybindings.properties";
	

	public static String getProperty(String key){

		loadProperties();
		return configuration.getProperty(key);
	}

	public static void setProperty(String prop, String value){
		loadProperties();
		configuration.setProperty(prop, value);
		try {
			FileOutputStream fos = new FileOutputStream(confUrl);
			configuration.store(fos, "---Configuration---");
			fos.close();
		} catch (FileNotFoundException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		} catch (IOException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		}
	}

	private static void loadProperties(){
		if(configuration==null){
			configuration = new Properties();
			InputStream is = null;

			try {
				is=new FileInputStream(confUrl);
				configuration.load(is);
				is.close();
				for (Enumeration e = configuration.keys(); e.hasMoreElements() ; ) {
					// Obtenemos el objeto
					Object obj = e.nextElement();
					ApplicationLogger.getLogger().log(Level.INFO, obj + ": " + configuration.getProperty(obj.toString()));
				}
			} catch(IOException ioe) {

				ApplicationLogger.getLogger().log(Level.SEVERE, ioe.getMessage());
			}

		}
	}


	public static String getXlsProperty(String key){

		loadXlsProperties();
		return xlsConfiguration.getProperty(key);
	}

	public static void setXlsProperty(String prop, String value){
		loadXlsProperties();
		xlsConfiguration.setProperty(prop, value);
		try {
			FileOutputStream fos = new FileOutputStream(xlsUrl);
			xlsConfiguration.store(fos, "---Configuration---");
			fos.close();
		} catch (FileNotFoundException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		} catch (IOException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		}
	}

	private static void loadXlsProperties(){
		if(xlsConfiguration==null){
			xlsConfiguration = new Properties();
			InputStream is = null;

			try {
				is=new FileInputStream(xlsUrl);
				xlsConfiguration.load(is);
				is.close();
				for (Enumeration e = xlsConfiguration.keys(); e.hasMoreElements() ; ) {
					// Obtenemos el objeto
					Object obj = e.nextElement();
					ApplicationLogger.getLogger().log(Level.INFO, obj + ": " + xlsConfiguration.getProperty(obj.toString()));
				}
			} catch(IOException ioe) {

				ApplicationLogger.getLogger().log(Level.SEVERE, ioe.getMessage());
			}

		}
	
	}

	public static String getKeyBindingsProperty(String key){

		loadKeyBindingsProperties();
		return keyBindings.getProperty(key);
	}

	public static void setKeyBindingsProperty(String prop, String value){
		loadKeyBindingsProperties();
		keyBindings.setProperty(prop, value);
		try {
			FileOutputStream fos = new FileOutputStream(keybindingsUrl);
			keyBindings.store(fos, "---Configuration---");
			fos.close();
		} catch (FileNotFoundException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		} catch (IOException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		}
	}

	private static void loadKeyBindingsProperties(){
		if(keyBindings==null){
			keyBindings = new Properties();
			InputStream is = null;

			try {
				is=new FileInputStream(keybindingsUrl);
				keyBindings.load(is);
				is.close();
				for (Enumeration e = keyBindings.keys(); e.hasMoreElements() ; ) {
					// Obtenemos el objeto
					Object obj = e.nextElement();
					ApplicationLogger.getLogger().log(Level.INFO, obj + ": " + keyBindings.getProperty(obj.toString()));
				}
			} catch(IOException ioe) {

				ApplicationLogger.getLogger().log(Level.SEVERE, ioe.getMessage());
			}

		}
	}
	
	
	public static String getLangProperty(String key){

		loadLangProperties();
		return lang.getProperty(key);
	}

	public static void setLangProperty(String prop, String value){
		loadLangProperties();
		lang.setProperty(prop, value);
		try {
			FileOutputStream fos = new FileOutputStream(langUrl);
			lang.store(fos, "---Configuration---");
			fos.close();
		} catch (FileNotFoundException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		} catch (IOException e) {
			ApplicationLogger.getLogger().log(Level.SEVERE, e.getMessage());
		}
	}

	private static void loadLangProperties(){
		if(lang==null){
			lang = new Properties();
			InputStream is = null;

			try {
				is=new FileInputStream(langUrl);
				lang.load(is);
				is.close();
				for (Enumeration e = lang.keys(); e.hasMoreElements() ; ) {
					// Obtenemos el objeto
					Object obj = e.nextElement();
					ApplicationLogger.getLogger().log(Level.INFO, obj + ": " + lang.getProperty(obj.toString()));
				}
			} catch(IOException ioe) {

				ApplicationLogger.getLogger().log(Level.SEVERE, ioe.getMessage());
			}

		}
	}
	
	public static ArrayList<String> getBindingKeys(){
		loadKeyBindingsProperties();
		ArrayList<String> keys = new ArrayList<String>();
		for(Enumeration e =keyBindings.keys(); e.hasMoreElements();){
			Object obj = e.nextElement();
			keys.add((String)obj);
		}
		return keys;
	}


	




}
